<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Music/classes/PhpFox_ComponentEditSong.class.php, $Author:  $ */



/** Component for edit songs
 *
 * @version $Id: PhpFox_ComponentEditSong.class.php 409 2008-03-17 20:39:58Z phpfoxsv $
 * @package module.music
 */
class PhpFox_Mod_Music_ComponentEditSong extends BaseComponent
{
    function process()
    {
        $oSongItem    = &$this->_oModule->getItem('Song');
        /* @var $oMusicService PhpFox_Mod_Music_ItemAlbum */
        $oMusicService = &$this->_oModule->getService('Music');
        /* @var $oMusicService PhpFox_Mod_Music_ServiceMusic */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = &$this->_oModule->getTemplate();

        $oReq = &App::getRequest();

        $iSongId = $oReq->getInt('id');

        $sAction = $oReq->get('action');

        $oSecurityService->checkPerm('music', array('full', 'read_only'));
        $oSecurityService->checkAccess('music');

        $oSongItem->load($iSongId);
        $oCurUser = App::loadUser();
        if(!$oSecurityService->isStaff() AND $oSongItem->aData['user_id']!=$oCurUser->aData['id'])
        {
        	App::gotoUrl('public.music');
        }
         
        App::loadClass('Validator');
        $aFields = array
        (
            'val[musician_name]' => array('title' => App::format('Mod_Music.musician_name'), 'def' => 'required'),
            'val[song_title]'   => array('title' => App::format('Mod_Music.album_title'), 'def' => 'required'),
        );
        $oValidator = &new Validator($aFields);


		if ( $oReq->get('update') )
		{
			$oSecurityService->checkPerm('music', array('full'));
            $aValues = $oReq->getArray('val');
			if (!$oValidator->isValid($oReq->getAll()))
            {
				$this->addErrors($oValidator->getErrors());
			}
			if (isset($_FILES['song']['name']) && (substr($_FILES['song']['name'], -3) != 'mp3'))
            {
            	$this->addErrors(App::format('Mod_Music.error.wrong_format'));
            }
            if (!count($this->getErrors()))
            {
            	$aSongData = array(
                	'musician_name'     => htmlspecialchars($aValues['musician_name']),
                    'song_title'        => htmlspecialchars($aValues['song_title']),
                    'genre_id'          => $aValues['genre_sel'],
				);

                if (!$oMusicService->saveSong($iSongId, $aSongData))
                {
					$this->addErrors($oMusicService->getErrors());
				}
            }
			App::gotoUrl('public.account.myitems.editmusic.editsong', array('alid' => $oReq->get('alid'), 'id' => $oReq->get('id')), App::format('global_updated'));
		}

		/*
        switch ($sAction){
            case 'Save changes':
                $oSecurityService->checkPerm('music', array('full'));
                $aValues = $oReq->getArray('val');
                if (!$oValidator->isValid($oReq->getAll()))
                {
                    $this->addErrors($oValidator->getErrors());
                }
                if (!count($this->getErrors())){
                    $aSongData = array(
                        'musician_name'     => htmlspecialchars($aValues['musician_name']),
                        'song_title'        => htmlspecialchars($aValues['song_title']),
                        'genre_id'          => $aValues['genre_sel'],
                    );
                    if (!$oMusicService->saveSong($iSongId, $aSongData)){
                        $this->addErrors($oMusicService->getErrors());
                    }
                }
                break;
            case 'delete_song':
                $oSecurityService->checkPerm('music', array('full'));
                if (!$oMusicService->deleteSong($iSongId)){
                    $this->addErrors($oMusicService->getErrors());
                }

                break;
            default:
                $sAction = '';
                break;
        }
        */

        list($aSong,$iCnt)   = $oMusicService->getSongsList(array('song_id'=>'='.$iSongId));

        $aAlbum = $oMusicService->getAlbumsList(array('album_id' => '= '.intval($aSong[0]['album_id'])));

        $aSong[0]['song_album'] = (isset($aAlbum[0][0]['album_title']) ? $aAlbum[0][0]['album_title'] : '');

		$this->oTpl->assignSrc('sValMess', $oValidator->makeJsMess());
        $this->oTpl->assignSrc('sValJs', $oValidator->makeJS());
        $oTpl->assign(array(
            'aSong'          => $aSong[0],
            'aGenres'        => $oMusicService->getGenres(),
            'sCurrentGenre'  => $aSong[0]['genre_id'],
            'sBack'          => $oReq->get('back'),
            'iAlbumId'       => $oReq->get('alid'),
        ));
        return $oTpl->fetch('EditSong.html');
    }
}
?>